package fiuba.webapp.action;

import com.opensymphony.xwork2.Preparable;
import fiuba.service.GenericManager;
import fiuba.model.Estructura;
import fiuba.model.ListaNoticias;
import fiuba.model.Seccion;
import fiuba.webapp.action.BaseAction;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ListaNoticiasAction extends BaseAction implements Preparable {
    private GenericManager<ListaNoticias, Long> listaNoticiasManager;
    private List listaNoticiass;
    private ListaNoticias listaNoticias;
    private Long  id;
    
    public void setListaNoticiasManager(GenericManager<ListaNoticias, Long> listaNoticiasManager) {
        this.listaNoticiasManager = listaNoticiasManager;
    }

    public List getListaNoticiass() {
        return listaNoticiass;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String listaNoticiasId = getRequest().getParameter("listaNoticias.id");
            if (listaNoticiasId != null && !listaNoticiasId.equals("")) {
                listaNoticias = listaNoticiasManager.get(new Long(listaNoticiasId));
            }
        }
    }
    public Map<String,String> getSizes() {
		Map<String,String> ret = new LinkedHashMap<String,String>();		    
	     for (int i = 5 ; i < 24; i ++ ){
		   	ret.put(i + "",i + " px.");
	     }
		return ret;
    }
    
    public Map<String,String> getAlineaciones(){
    	Map<String,String> ret = new LinkedHashMap<String,String>();
	   	ret.put("left", "Izquierda");
	   	ret.put("centre", "Centrado");
	   	ret.put("right", "Derecha");
    	return ret;
    }
    
    public String list() {
        listaNoticiass = listaNoticiasManager.getAll();
        return SUCCESS;
    }

    public void setId(Long  id) {
        this. id =  id;
    }

    public ListaNoticias getListaNoticias() {
        return listaNoticias;
    }

    public void setListaNoticias(ListaNoticias listaNoticias) {
        this.listaNoticias = listaNoticias;
    }

    public String delete() {
        listaNoticiasManager.remove(listaNoticias.getId());
        saveMessage(getText("listaNoticias.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            listaNoticias = listaNoticiasManager.get(id);
        } else {
            listaNoticias = new ListaNoticias();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (listaNoticias.getId() == null);  
        listaNoticiasManager.save(listaNoticias);

        String key = (isNew) ? "listaNoticias.added" : "listaNoticias.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}